home *** CD-ROM | disk | FTP | other *** search
- /*
- * getopt.h : routine to handle options from the argument vector.
- * Source : writen by Thomas Hadig based on getopt.h v1.1 by aklevin
- *
- * Version 1.0, January 1993
- */
- /*{{{}}}*/
- /*{{{ description*/
- /*
- * getopt uses four variables :
- * - optarg points to an option's argument (if any).
- * - optind holds the index of the next argument vector element to parse.
- * Once all options have been parsed, points to the first non-option
- * argument. [If (optind > argc) then there are no more arguments].
- * - opterr, if set to 0 will suppress getopt's error messages
- * (default is 1).
- * - optopt, while not usually documented, is used here to return the actual
- * option character found, even when getopt itself returns '?'.
- *
- * getopt will parse arguments like :
- * -o
- * -oooooo....
- * -o string
- * -ostring
- *
- * getopt will stop, if there is
- * - no '-' before the next argument of the list (will not be skipped)
- * - is a single '-' (will not be skipped)
- * - is a double '--' (will be skipped)
- *
- */
- /*}}} */
-
- #ifndef GETOPT_H
-
- /*{{{ defines*/
- #define GETOPT_H
-
- #ifndef public
- #define public
- #endif
- #ifndef private
- #define private static
- #endif
- /*}}} */
- /*{{{ includes*/
- #ifndef _STDIO_H
- #include <stdio.h>
- #endif
- #ifndef _STRING_H
- #include <string.h>
- #endif
- /*}}} */
- /*{{{ variables*/
- public char *optarg; /* contains argument, if needed */
- public int optind=1; /* Index of Argument, where getopt will look in */
- public int opterr=1; /* contains error-print-flag */
- public int optopt; /* contains option char */
- /*}}} */
-
- /*{{{ getopt*/
- public int getopt (int argc, char **argv, char *optstring)
- {
- int any_more, i=0, result=EOF;
- static int opthold; /* last line, where looked for option */
- static int optsub=1; /* store position for second option, if there */
-
- optarg = NULL;
-
- /*{{{ Reset optsub if caller has changed optind*/
- if (optind != opthold) optsub = 1;
- /*}}} */
- /*{{{ Look at argument vector*/
- while ((!i)&&(optind<argc)&&(result==EOF))
- {
- if ((argv[optind][0] == '-') && (argv[optind][1] != '\0'))
- /*{{{ argument (index optind) contains options ( '-'+chars )*/
- {
- if (argv[optind][1] == '-')
- /*{{{ skip argument '--'*/
- {
- optind++;
- i=1;
- }
- /*}}} */
- else
- /*{{{ handle option*/
- {
- /*{{{ Look for option ...*/
- i=0;
- while ((i<strlen(optstring))&&(result==EOF))
- {
- if ( (optopt = argv[optind][optsub]) == optstring[i])
- /*{{{ option found*/
- {
- /*{{{ is there a char behind the option ?*/
- any_more = strlen(argv[optind])-optsub-1;
- /*}}} */
- if (optstring[i+1] == ':')
- /*{{{ argument needed*/
- {
- if (optind == argc-1 && !any_more)
- /*{{{ no arguments and no chars left -> missing argument*/
- {
- if (opterr) fprintf(stderr,
- "%s: `-%c' option requires an argument.\n",argv[0], optopt);
- optind++;
- result='?';
- }
- /*}}} */
- else
- /*{{{ set optarg correct and change optind*/
- {
- /*{{{ chars behind option -> No, use next argument*/
- if (!any_more) optarg = argv[++optind];
- /*}}} */
- /*{{{ ...Yes*/
- else optarg = &argv[optind][optsub+1];
- /*}}} */
- optind++;
- optsub=1;
- }
- /*}}} */
- }
- /*}}} */
- else
- /*{{{ No*/
- {
- /*{{{ no chars follow -> next option in next argument*/
- if (!any_more)
- {
- optind++;
- optsub=1;
- }
- /*}}} */
- /*{{{ Yes*/
- else optsub++;
- /*}}} */
- }
- /*}}} */
- result=optopt;
- }
- /*}}} */
- i++;
- }
- i=0;
- /*}}} */
- if (result==EOF)
- /*{{{ not found*/
- {
- /*{{{ print Unknown option*/
- if (opterr)
- fprintf(stderr, "%s: Unrecognized option `-%c'.\n", argv[0], optopt);
- /*}}} */
- /*{{{ are there more options ? Yes -> set optsub*/
- if (strlen(argv[optind])-optsub-1) optsub++;
- /*}}} */
- else
- /*{{{ No*/
- {
- optind++;
- optsub=1;
- }
- /*}}} */
- result='?';
- }
- /*}}} */
- }
- /*}}} */
- }
- /*}}} */
- else i=1;
- }
- /*}}} */
- opthold = optind;
- return(result);
- }
- /*}}} */
- #endif
-